<?php

namespace App\Modules\Admin\Http\Controllers;

use App\Http\Controllers\BaseController;
use App\Services\NewsService;
use Illuminate\Http\Request;

class NewsController extends BaseController
{
    public function __construct(NewsService $newsService)
    {
        $this->middleware('check.adminLogin')->except('index');

        $this->newsService = $newsService;
    }

    public function index()
    {
        $param = $this->validationParameter([
            'page|页码' => 'noRequire|number|default:1',
            'per_page|显示数量' => 'noRequire|number|default:30',
        ]);

        $data = $this->newsService->getList($param);

        return $this->response->success($data);
    }

    public function show($id)
    {
        // $id = $request->input('id');
        $result = $this->newsService->getInfo($id);

        return $this->response->success($result);
    }

    public function store(Request $request)
    {
        $param = $this->validationParameter([
            'title|标题' => 'require',
            'image|图片' => 'require',
            'type|类型' => 'require|number|default:1',
            'content|内容' => 'require',
        ]);

        $param['publisher_uid'] = $request->user()->admin_uid ?? null;

        $result = $this->newsService->add($param);

        return $this->response->success($result);
    }

    public function update(Request $request)
    {
        $id = $request->input('id');
        $param = [];
        $result = $this->newsService->update($param, $id);

        return $this->response->success($result);
    }

    public function destroy(Request $request)
    {
        $id = $request->input('id');
        $result = $this->newsService->delete($id);

        return $this->response->success($result);
    }
}
